using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spinning.Spinning.Entity.Scenes;
using Microsoft.Xna.Framework;
using Spinning.Spinning.Unitity;

namespace Spinning.Spinning.Entity
{
    class WindBladeShoter : BaseEntity
    {
        SinglePlayerScene _scene;
        public List<WindBlade> blades;
        public WindBladeShoter(SinglePlayerScene scene)
        {
            _scene = scene;
            blades = new List<WindBlade>();
            _scene.UpdateHandlerListener += recycle;
        }

        public override void OnLoad()
        {
            
        }

        public void Shot(Vector2 pos, float velocity)
        {
            shotSingleBlade(Spinning.Entity.WindBlade.ShotDirection.down, velocity, pos);
            shotSingleBlade(Spinning.Entity.WindBlade.ShotDirection.up, velocity, pos);
            shotSingleBlade(Spinning.Entity.WindBlade.ShotDirection.left, velocity, pos);
            shotSingleBlade(Spinning.Entity.WindBlade.ShotDirection.right, velocity, pos);
        }

        void shotSingleBlade(Spinning.Entity.WindBlade.ShotDirection direction, float velocity, Vector2 pos)
        {
            WindBlade wd = new WindBlade();
            wd.OnLoad();
            wd.OnCreatBody(_scene.World, _scene.PhysicConnectManager);
            wd.BodyPosiotion = pos;
            wd.ZIndex = Constants.enemy;
            wd.Velocity = velocity;
            wd.Shot(direction);
            _scene.Attach(wd);
            blades.Add(wd);
        }

        protected override void UpdateSelf(GameTime gameTime)
        {
            base.UpdateSelf(gameTime);
            
        }

        private void recycle(GameTime gameTime)
        {
            List<WindBlade> temp = new List<WindBlade>();

            for (int i = 0; i < blades.Count; i++)
            {
                WindBlade wd = blades[i];
                if (wd.IsOutOfView())
                {
                    _scene.World.DestroyBody(wd.body);
                    _scene.Detach(wd);
                    temp.Add(wd);
                }
            }

            foreach (WindBlade wd in temp)
                blades.Remove(wd);
        }



    }
}
